package com.irdstudio.efp.console.api.rest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.irdstudio.basic.framework.core.constant.*;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.console.common.LimitStatusEnum;
import com.irdstudio.efp.console.service.facade.QuotaLimitPrdService;
import com.irdstudio.efp.console.service.vo.QuotaLimitPrdVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.console.service.facade.CustomerLimitInfoService;
import com.irdstudio.efp.console.service.vo.CustomerLimitInfoVO;

/**
  *
  * 客户限额
  * @author wanglihua
  * @since 2019年3月13日 下午3:25:20
  * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class CustomerLimitInfoController extends AbstractController {
	
	@Autowired
	@Qualifier("customerLimitInfoService")
	private CustomerLimitInfoService customerLimitInfoService;
	

	
	/**
	 * 
	 * TODO 查询信息<br>
	 * 0.2:wanglihua:2019年3月13日 下午3:25:20:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月13日 下午3:25:20
	 * @date 2019年3月13日 下午3:25:20
	 * @version 0.1 
	 */
	@PostMapping(value = "/quoat/limit/infos")
	public @ResponseBody ResponseData<List<CustomerLimitInfoVO>> queryAllByPage(@RequestBody CustomerLimitInfoVO vo) {
		List<CustomerLimitInfoVO> outputVo = customerLimitInfoService.queryAllByPage(vo);
		checkData(outputVo);
		return getResponseData(outputVo);
	}

	private void checkData(List<CustomerLimitInfoVO> outputVo) {
		try {
			for (CustomerLimitInfoVO item : outputVo) {
				int dateNum = compareDate(DateUtility.getCurrDate(), item.getEndDt());
				if (LimitStatusEnum.QUOTA_LIMIT_STATUS_01.getKey().equals(item.getSts())){
					if (dateNum < 0) {
						item.setSts(LimitStatusEnum.QUOTA_LIMIT_STATUS_00.getKey());
						logger.info("更新超出有效期的信息为无效。");
						CustomerLimitInfoVO customerLimitInfoVO = new CustomerLimitInfoVO();
						customerLimitInfoVO.setSts(LimitStatusEnum.QUOTA_LIMIT_STATUS_00.getKey());
						customerLimitInfoVO.setSerno(item.getSerno());
						customerLimitInfoService.updateQuotaTypeByPk(customerLimitInfoVO);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

	private int compareDate(String currDate, String endDt) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateFormatConstant.DATE_FORMAT);
		int result = 0;
		try {
			long d1 = sdf.parse(currDate).getTime();
			long d2 = sdf.parse(endDt).getTime();
			if (d1 > d2) {
				result = -1;
			} else if (d1 == d2) {
				result = 0;
			} else if (d1 < d2) {
				result = 1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * TODO 根据主键删除信息<br>
	 * 0.2:wanglihua:2019年3月13日 下午3:25:20:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月13日 下午3:25:20
	 * @date 2019年3月13日 下午3:25:20
	 * @version 0.1 
	 */
	@RequestMapping(value="/quoat/limit/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CustomerLimitInfoVO customerLimitInfoVO) {		
		int outputVo = customerLimitInfoService.deleteByPk(customerLimitInfoVO);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 
	 * TODO 根据主键更新信息<br>
	 * 0.2:wanglihua:2019年3月13日 下午3:25:20:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月13日 下午3:25:20
	 * @date 2019年3月13日 下午3:25:20
	 * @version 0.1 
	 */
	@RequestMapping(value="/quoat/limit/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CustomerLimitInfoVO customerLimitInfoVO) {		
		int outputVo = customerLimitInfoService.updateByPk(customerLimitInfoVO);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 
	 * TODO 新增数据<br>
	 * 0.2:wanglihua:2019年3月13日 下午3:25:20:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月13日 下午3:25:20
	 * @date 2019年3月13日 下午3:25:20
	 * @version 0.1 
	 */
	@RequestMapping(value="/quoat/limit/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCustomerLimitInfo(@RequestBody CustomerLimitInfoVO customerLimitInfoVO) {
		int outputVo = customerLimitInfoService.insertCustomerLimitInfo(customerLimitInfoVO);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 
	 * TODO 根据主键更新状态<br>
	 * 0.2:wanglihua:2019年3月13日 下午3:25:20:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月13日 下午3:25:20
	 * @date 2019年3月13日 下午3:25:20
	 * @version 0.1 
	 */
	@RequestMapping(value="/quoat/limit/infos/status", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateQuotaTypeByPk(@RequestBody CustomerLimitInfoVO customerLimitInfoVO) {
		int outputVo = customerLimitInfoService.updateQuotaTypeByPk(customerLimitInfoVO);
		return getResponseData(outputVo);
	}

	@RequestMapping(value="/quoat/limit/prd/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertLimitPrdInfo(@RequestBody QuotaLimitPrdVO quotaLimitPrdVO) {
		setUserInfoToVO(quotaLimitPrdVO);
		quotaLimitPrdVO.setPrdId(quotaLimitPrdVO.getPrdCode());
		quotaLimitPrdVO.setCreateUser(quotaLimitPrdVO.getLoginUserId());
		quotaLimitPrdVO.setCreateTime(TimeUtil.getCurrentDateTime());
		quotaLimitPrdVO.setLastChgTime(TimeUtil.getCurrentDateTime());
		quotaLimitPrdVO.setLastChgUsr(quotaLimitPrdVO.getLoginUserId());
		int outputVo = quotaLimitPrdService.insert(quotaLimitPrdVO);
		return getResponseData(outputVo);

	}

	@RequestMapping(value="/quoat/limit/prd/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateLimitPrdInfoByPk(@RequestBody QuotaLimitPrdVO quotaLimitPrdVO) {
		setUserInfoToVO(quotaLimitPrdVO);
		quotaLimitPrdVO.setLastChgTime(TimeUtil.getCurrentDateTime());
		quotaLimitPrdVO.setLastChgUsr(quotaLimitPrdVO.getLoginUserId());
		int outputVo = quotaLimitPrdService.updateByPk(quotaLimitPrdVO);
		return getResponseData(outputVo);
	}

	@RequestMapping(value="/quoat/limit/prd/infos/status", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateQuotaStsByPk(@RequestBody QuotaLimitPrdVO quotaLimitPrdVO) {
		setUserInfoToVO(quotaLimitPrdVO);
		quotaLimitPrdVO.setLastChgUsr(quotaLimitPrdVO.getLoginUserId());
		quotaLimitPrdVO.setLastChgTime(TimeUtil.getCurrentDateTime());
		int outputVo = quotaLimitPrdService.updateQuotaStsByPk(quotaLimitPrdVO);
		return getResponseData(outputVo);
	}

	@PostMapping(value = "/quoat/limit/prd/infos")
	public @ResponseBody ResponseData<List<QuotaLimitPrdVO>> queryPrdLimitQuoatByPage(@RequestBody QuotaLimitPrdVO quotaLimitPrdVO) {
		List<QuotaLimitPrdVO> outputVo = null;
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		quotaLimitPrdVO.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(quotaLimitPrdVO);
		switch (dataRuleType) {
			case Owner:
				outputVo = quotaLimitPrdService.queryAllByPage(quotaLimitPrdVO);
				break;
			case CurrOrg:
				outputVo = quotaLimitPrdService.queryAllByPage(quotaLimitPrdVO);
				break;
			case CurrDownOrg:
				outputVo = quotaLimitPrdService.queryAllByPage(quotaLimitPrdVO);
				break;
			case CurrOwnerPrd:
				outputVo = quotaLimitPrdService.queryAllCurrPrdByPage(quotaLimitPrdVO);
				break;
			default:
				outputVo = quotaLimitPrdService.queryAllByPage(quotaLimitPrdVO);
				break;
		}
        checkPrdData(outputVo);
		return getResponseData(outputVo);
	}

    private void checkPrdData(List<QuotaLimitPrdVO> outputVo) {
        try {
            for (QuotaLimitPrdVO item : outputVo) {
                int dateNum = compareDate(DateUtility.getCurrDate(), item.getEndDt());
                if (LimitStatusEnum.QUOTA_LIMIT_STATUS_01.getKey().equals(item.getSts())){
                    if (dateNum < 0) {
                        item.setSts(LimitStatusEnum.QUOTA_LIMIT_STATUS_00.getKey());
                        logger.info("更新超出有效期的信息为无效。");
                        QuotaLimitPrdVO quotaLimitPrdVO = new QuotaLimitPrdVO();
                        quotaLimitPrdVO.setSts(LimitStatusEnum.QUOTA_LIMIT_STATUS_00.getKey());
                        quotaLimitPrdVO.setSerno(item.getSerno());
                        quotaLimitPrdService.updateQuotaStsByPk(quotaLimitPrdVO);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }

	@Autowired
	@Qualifier("quotaLimitPrdService")
	private QuotaLimitPrdService quotaLimitPrdService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

}
